package org.unikn.dbvis.tools;

import java.util.ArrayList;
import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

public class GetLocations extends DefaultHandler {
	public static ArrayList<Float> longitude =  new ArrayList<Float>();
	public static ArrayList<Float> latitude =  new ArrayList<Float>();
	public static ArrayList<Integer> tonality = new ArrayList<Integer>();
	public static ArrayList<Integer> category = new ArrayList<Integer>();
	public static HashMap<Integer,String> categories = new HashMap<Integer,String>();
	public static ArrayList<String> date = new ArrayList<String>();
	public static HashMap<String,Integer> days = new HashMap<String, Integer>();
	public static String[] daynames = new String[16];
	private int i=0;
	private int j = 0;
	private String lastDay;
	private String d;
  public GetLocations () {
  	super();
  	
  }

  public final void startElement (final String uri, final String localName,
      final String qName, final Attributes attributes) {
  	if (qName == "g") {
  		longitude.add(Float.parseFloat(attributes.getValue("lo")));
  		latitude.add(Float.parseFloat(attributes.getValue("la")));
  		return;
  	}
  	if  (qName=="t")
  	{
  		tonality.add(Integer.parseInt(attributes.getValue("v")));
  		return;
  	}
  	if  (qName=="c")
  	{
  		final String c = attributes.getValue(0).toLowerCase();
  		if(categories.containsValue(c))
  		{
  			Integer key=0;
  			for(Integer key21 : categories.keySet()){
  				if(categories.get(key21).equals(c))
  				{key = key21;
  					break;
  				}
  			}
  			category.add(key.intValue());
  		}else
  		{
  			final int key = categories.size();
  			categories.put(key, c);
  			category.add(key);
  		}
  		
  		return;
  	}
  	if  (qName=="d")
  	{
  		d = attributes.getValue("v");

  		if(!d.equals(lastDay)){
  			days.put(d, new Integer(i));
  			lastDay = d;
  			daynames[j] = d;
  			j++;
  		}
  		date.add(d);
  		i++;
  		return;

  	}
  	
  }

	public static int getDay(final int i) {
		// TODO Auto-generated method stub
		String tmpname = date.get(i);
		int j = 0;
		for(String d : daynames){
			
			if(d.equals(tmpname)) 
				return j++;
			j++;
		}
		return 15;
	}

	public static ArrayList<Integer> getCatByName(final String name)
	{
	
		ArrayList<Integer> cats = new ArrayList<Integer>();
		for(final Integer d : categories.keySet() ){
			
			if(categories.get(d).contains(name.toLowerCase())) {
				cats.add(d);
			}
		}
		ArrayList<Integer> ret = new ArrayList<Integer>();
		for(final Integer i : cats){
			ret.add( i);
		}
		return ret;
	}
	
	public static String getCat(final int i) {
		String tmpname = categories.get(i);
		return tmpname;
	}

	public static int getCatId(final int i2) {
		// TODO Auto-generated method stub
		return category.get(i2);
	}
}
